
/*
 
 OpenGL GLUT template for Xcode 3.
 
 */
//Duplicate symbol display()    in /Users/zachrythayer/Desktop/Programming/PCDev/rayTracer/build/rayTracer.build/Debug/rayTracer.build/Objects-normal/x86_64/main-C9658372E0021E58.o and /Users/zachrythayer/Desktop/Programming/PCDev/rayTracer/build/rayTracer.build/Debug/rayTracer.build/Objects-normal/x86_64/main-C9658372E0021E58.o


#include <GLUT/glut.h>
#include <stdio.h>

#include "Scene.h"
#include "gwTimer.h"

void init(void);
void display(void);
void idle(void);
void reshape(int, int);
void mouse(int, int, int, int);

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /* Sets the window to double buffered with RGB colour profile */
	glutInitWindowSize(800, 600); /* Sets the size of the window */
	glutInitWindowPosition(100, 100); /* Sets the window position */
	glutCreateWindow("Ray Trace"); /* Creates the window with title GLUT Template */
	init();
	glutDisplayFunc(display);
	glutIdleFunc(idle);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMainLoop();
	
    return 0;
}

Scene scn;
gwTimer timer;
Sphere *s, *s2, *s3, *s4, *s5;
Light l,l2;
float var=0;
void anim(void *pData){
	gwTimer *t = reinterpret_cast<gwTimer*> (pData);
	//float p = 1.f/t->
	
	var += t->deltaTime()/t->fps();
	
	//Vec3 scnT(0,-4*sinf(var),0);
	// l.moveTo(scnT);
	//l.moveTo(scnT);
	//Vec3 scnT2(4*sinf(var),1,-3);
	//l2.moveTo(scnT2);
	Vec3 scnT3(3*sinf(var),0,3*cosf(var));
	s4->moveTo(scnT3);
	
	Vec3 scnT2(0,3*sinf(var),3*cosf(var));
	s5->moveTo(scnT2);
}

void init(void)
{
	timer.newEvent(1.f/60.f,anim,&timer);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
	
	Material m;
	Vec3 sPos(0.8f,1,0);
	Vec3 sRot(0,0,0);
	Vec3 sScl(1,1,1);
	
	m.b = 1.0f;
	m.r = 0.f;
	m.g = 1.0f;
	m.reflect = 0.5f;
	s = new Sphere(sPos, sScl, sRot, m);
	
	m.b = 0.0f;
	m.r = 1.f;
	m.g = 1.f;
	sPos.x = -0.8f;
	m.reflect = 0.5f;
	s2 = new Sphere(sPos, sScl, sRot, m);
	
	m.b = 1.0f;
	m.r = 1.0f;
	m.g = 0.0f;
	sPos.x = 0;
	sPos.y = -0.6f;
	sPos.z = 0;
	sScl.x = 1.f;
	m.reflect = 0.5f;
	s3 = new Sphere(sPos, sScl, sRot, m);
	
	sScl.x = 1;
	sPos.y = 0;
	sPos.z = 0;
	m.r = 0.4f;
	m.g = 0.7f;
	m.b = 0.2f;
	m.reflect = 0.0f;
	s4 = new Sphere(sPos, sScl, sRot, m);
	
	sScl.x = 1;
	sPos.y = 0;
	sPos.z = 0;
	m.r = 0.0f;
	m.g = 0.0f;
	m.b = 0.0f;
	m.reflect = 1.0f;
	s5 = new Sphere(sPos, sScl, sRot, m);
	

	Vec3 lPos(-3,0.5f,-1.f);
	l.moveTo(lPos);
	
	lPos.x = 0;
	lPos.y = 2.f;
	lPos.z = -10.f;
	l2.moveTo(lPos);
	
	Material lm;
	lm.r = 0.6f;
	lm.g = 0.7f;
	lm.b = 1.0f;
	l.setMaterial(lm);
	lm.r = 0.8f;
	lm.g = 0.8f;
	lm.b = 1.f;
	l2.setMaterial(lm);

	lPos.x = 1.0f;
	l.setScale(lPos);
	lPos.x = 0.9f;
	l2.setScale(lPos);


	
	scn.addLight(&l);
	scn.addLight(&l2);

	scn.addObject(s);
	scn.addObject(s2);
	scn.addObject(s3);
	scn.addObject(s4);
	scn.addObject(s5);

	scn.draw();
	//scn.saveRender("yay.tga")
	
}

char fpsStr[255];
int cnt = 0;
void idle(void){
	
	display();
	
	timer.update();
	cnt++;
	sprintf(fpsStr, "Ray Trace | fps:%04f | objCount:%d ",timer.fps(), scn.objectCount());
	glutSetWindowTitle(fpsStr);
//	sprintf(fpsStr, "%d.tga", cnt);
//	scn.saveRender(fpsStr);
	
}

void display(void)
{
	scn.draw();//rayTrace!
	
	glClear(GL_COLOR_BUFFER_BIT);
	
	//glRasterPos2f(scn.getW()*0.5,scn.getH()*0.5);
	void *buf = scn.getBuffer();
	glDrawPixels(scn.getWidth(), scn.getHieght(), GL_RGBA, GL_UNSIGNED_BYTE, buf);
	
	glutSwapBuffers();
	
}

void reshape(int w, int h)
{
	/* window reshape function */
}

void mouse(int button, int state, int x, int y)
{
	/* mouse handling function */
}
